package edu.fh.salzburg.its.opsystems.newfs.definition;

import java.util.Date;
import java.util.EnumSet;

import edu.fh.salzburg.its.opsystems.newfs.exception.NewFSException;
import edu.fh.salzburg.its.opsystems.newfs.implementation.NewFSDirEntry;

/**
 * This class represents Elements in your filesystem.
 * you can extend this class as you want, adding features for your elements.
 * @author Michael Brachtl
 *
 */
public abstract class NewFSElement {

	private String identifer = "";
	private String path = "";
	private NewFSDirEntry dirEntry;

	public NewFSElement() {
		dirEntry = new NewFSDirEntry();
	}
	/**
	 * This gives a unique identifier of the element in the filesystem.
	 * @return
	 */
	public String getIdentifer() {
		return identifer;
	}

	/**
	 * Sets the unique identifier of the element in the filesystem.
	 * @param identifer
	 */
	public void setIdentifer(String identifer) {
		this.identifer = identifer;
	}

	/**
	 * This gives the name of the element - can be unique in the filesystem but not needed to
	 * @return the name
	 */
	public String getName() {
		return this.dirEntry.getName();
	}

	/**
	 * This sets the name of the element - can be unique in the filesystem but not needed to.
	 * @return the name
	 */
	public void setName(String name) {
		this.dirEntry.setName(name);
	}

	/**
	 * returns the complete directory entry object
	 * @return NewFSDirEntry
	 */
	public NewFSDirEntry getDirEntry() {
		return dirEntry;
	}

	/**
	 * sets the complete directory entry object
	 * @return NewFSDirEntry
	 */
	public void setDirEntry(NewFSDirEntry dirEntry) {
		this.dirEntry = dirEntry;
	}
	
	public void setFromByteArray(byte[] data) throws NewFSException {
		this.dirEntry.setFromByteArray(data);
	}

	public byte[] toByteArray() {
		return this.dirEntry.toByteArray();
	}

	public EnumSet<FileAttributes> getAttributes() {
		return this.dirEntry.getAttributes();
	}

	public void setAttributes(EnumSet<FileAttributes> attributes) {
		this.dirEntry.setAttributes(attributes);
	}

	public Date getBuild() {
		return this.dirEntry.getBuild();
	}

	public void setDatesNow() {
		Date dat = new Date();
		this.dirEntry.setBuild(dat);
		this.dirEntry.setLastaccess(dat);
		this.dirEntry.setLastmod(dat);
	}

	public void setBuild(Date build) {
		this.dirEntry.setBuild(build);
	}

	public Date getLastaccess() {
		return this.dirEntry.getLastaccess();
	}

	public void setLastaccess(Date lastaccess) {
		this.dirEntry.setLastaccess(lastaccess);
	}

	public Date getLastmod() {
		return this.dirEntry.getLastmod();
	}

	public void setLastmod(Date lastmod) {
		this.dirEntry.setLastmod(lastmod);
	}

	public int getStartCluster() {
		return this.dirEntry.getStartCluster();
	}

	public void setStartCluster(int startCluster) {
		this.dirEntry.setStartCluster(startCluster);
	}

	public int getSize() {
		return this.dirEntry.getSize();
	}

	public void setSize(int size) {
		this.dirEntry.setSize(size);
	}

	/**
	 * @return the parentDirectory
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param parentDirectory the parentDirectory to set
	 */
	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * @param parentDirectory the parentDirectory to set
	 */
	public void setPath(String[] path) {
		this.path = new String();
		for (int i=0; i<path.length; i++) {
			this.path = this.path.concat(path[i] + NewFSConstants.PATH_SEPARATOR);
		}
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return dirEntry.toString();
	}
}
